package pl.donajski.magisterka;

import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;

public class FileTaskLoader extends FileLoader {

	public FileTaskLoader(String fileName, List<Node> nodeContext) throws FileNotFoundException {
		super(fileName);
		this.mNodeContext = nodeContext;
		this.mIndex = 0;
	}

	public List<Task> load() {
		ArrayList<Task> tasksList = new ArrayList<Task>();
		int count = Integer.parseInt(this.next());

		while(count-- > 0) {
			tasksList.addAll(this.createProjectTasks());
		}
		
		return tasksList;
	}
	
	private List<Task> createProjectTasks() {
		int count 		= 0,
			inputTrans 	= 0,
			outputTrans = 0,
			destCount 	= 0,
			projectIdx  = 0;
		Node sourceNode = null;
		HashSet<Node> destinations 		= new HashSet<Node>();
		ArrayList<Task> projectTasks 	= new ArrayList<Task>();

		projectIdx  = Integer.parseInt(this.next()) - 1;
		count 		= Integer.parseInt(this.next());
		inputTrans 	= Integer.parseInt(this.next());
		outputTrans = Integer.parseInt(this.next());
		sourceNode 	= this.mNodeContext.get(Integer.parseInt(this.next())-1);
		destCount 	= Integer.parseInt(this.next());
		
		
		while(destCount-- > 0) {
			destinations.add(this.mNodeContext.get(
								Integer.parseInt(this.next())-1));
		}
		
		while(count-- > 0) {
			projectTasks.add(new Task(this.mIndex++, inputTrans, outputTrans,
				sourceNode, false, null, destinations, projectIdx));
		}
		
		return projectTasks;
	}
	
	private List<Node> mNodeContext;
	private int mIndex;
}
